package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.DeviceOptions;
import com.hzhr.iot.device.convertor.custom.CollectDeviceCustomConvertor;
import com.hzhr.iot.device.dao.CollectDeviceDao;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.dao.helper.CollectDeviceDaoHelper;
import com.hzhr.iot.device.dao.helper.IotDeviceDaoHelper;
import com.hzhr.iot.device.dao.mapper.custom.CollectDeviceEntityCustomMapper;
import com.hzhr.iot.device.entity.CollectDeviceEntity;
import com.hzhr.iot.device.entity.ext.CollectDeviceEntityExt;
import com.hzhr.iot.device.model.collectDevice.CollectDeviceGetModel;
import com.hzhr.iot.device.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.model.collectDeviceQuickquery.CollectDeviceQuickqueryItemModel;
import com.hzhr.iot.device.model.collectDeviceQuickquery.CollectDeviceQuickqueryModel;
import com.hzhr.iot.device.model.collectDeviceQuickquery.MeterPlan_workOrderItemModel;
import com.hzhr.iot.device.model.collectDeviceQuickquery.MeterPlan_workOrderlistModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.device.service.helper.*;
import com.hzhr.iot.toll.sdk.service.Toll_GasUserService;
import com.hzhr.iot.user.sdk.service.User_CompanyService;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

@Service
@CygService(entity = CollectDeviceEntity.class, model = CollectDeviceModel.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),
        @CygQuery(name = "_equal_code", query = "equal_code", type = String.class),
        @CygQuery(name = "_equal_companyId", query = "equal_companyId", type = Integer.class),
        @CygQuery(name = "_equal_deptId", query = "equal_deptId", type = Integer.class),
        @CygQuery(name = "_equal_deptCode", query = "equal_deptCode", type = String.class),
        @CygQuery(name = "_equal_categoryCode", query = "equal_categoryCode", type = String.class),
        @CygQuery(name = "_like_code", query = "like_code", type = String.class),
        @CygQuery(name = "_equal_iotDeviceId", query = "equal_iotDeviceId", type = Integer.class),
        @CygQuery(name = "_like_iotDeviceCode", query = "like_iotDeviceExt_code", type = String.class),
        @CygQuery(name = "_like_gasUserName", query = "like_gasUserName", type = String.class),
        @CygQuery(name = "_like_gasUserCode", query = "like_gasUserCode", type = String.class),
        @CygQuery(name = "_like_gasUserAddress", query = "like_gasUserAddress", type = String.class),
        @CygQuery(name = "_greaterthanorequal_createTime", query = "greaterthanorequal_createTime", type = Date.class),
        @CygQuery(name = "_lessthan_createTime", query = "lessthan_createTime", type = Date.class),
        @CygQuery(name = "_equal_gasUserId", query = "equal_gasUserId", type = Integer.class),
        @CygQuery(name = "_equal_prodFactoryId", query = "equal_typeExt_prodFactoryId", type = Integer.class),
        @CygQuery(name = "_equal_iotDeviceCompanyName", query = "equal_iotDeviceExt_companyExt_name", type = String.class),
        @CygQuery(name = "_equal_gasUserCode", query = "equal_gasUserCode", type = String.class),
        @CygQuery(name = "_like_deptCode", query = "like_deptCode", type = String.class),
        @CygQuery(name = "_equal_iotDeviceCompanyId", query = "equal_iotDeviceExt_companyId", type = Integer.class),
        @CygQuery(name = "_equal_prodFactoryIotCompanyId", query = "equal_typeExt_prodFactoryExt_iotCompanyId", type = Integer.class),

})
public class CollectDeviceQuickqueryService extends BaseService {
    @Autowired
    CollectDeviceCustomConvertor collectDeviceCustomConvertor;
    @Autowired
    CollectDeviceDao collectDeviceDao;
    @Autowired
    CollectDeviceQuickqueryService collectDeviceService;
    @Autowired
    User_DeptService user_deptService;
    @Autowired
    Toll_GasUserService toll_gasUserService;
    @Autowired
    IotDeviceService iotDeviceService;
    @Autowired
    User_CompanyService user_companyService;
    @Autowired
    CollectDataService collectDataService;
    @Autowired
    IotDeviceDao iotDeviceDao;
    @Autowired
    DeviceOptions deviceOptions;
    @Autowired
    IotDeviceTypeServiceHelper iotDeviceTypeServiceHelper;
    @Autowired
    IotDeviceServiceHelper iotDeviceServiceHelper;
    @Autowired
    CollectDeviceServiceHelper collectDeviceServiceHelper;
    @Autowired
    User_DeptServiceHelper user_deptServiceHelper;
    @Autowired
    Toll_GasUserServiceHelper toll_gasUserServiceHelper;
    @Autowired
    IotDeviceDaoHelper iotDeviceDaoHelper;
    @Autowired
    CollectDeviceDaoHelper collectDeviceDaoHelper;
    @Autowired
    User_CompanyServiceHelper user_companyServiceHelper;
    @Autowired
    CollectDeviceEntityCustomMapper collectDeviceEntityCustomMapper;



    //region javadoc注释 : getQuick
    /**
     * 查询采集设备 <br/><br/>
     * 简介 : 快速查询采集设备（个性化定制方法，专为提升速度） <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _equal_collectTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : CollectDeviceQuickqueryItemModel.currentTm处于#{equal_collectTime}所在的那一天内;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see MeterPlan_workOrderlistModel body <br/>
     * @see CollectDeviceQuickqueryModel result <br/>
     */ 
    //endregion
    //region 说明 : getQuick
    //查询采集设备
    //简介 : 快速查询采集设备（个性化定制方法，专为提升速度）
    //endregion
    //region 参数 : getQuick
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "equal_collectTime", value = "CollectDeviceQuickqueryItemModel.currentTm处于#{equal_collectTime}所在的那一天内",required = false, dataType = "string", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_equal_collectTime", value = "CollectDeviceQuickqueryItemModel.currentTm处于#{equal_collectTime}所在的那一天内",required = false, dataType = "string", paramType = "query"),

    })
    //endregion
    //region 请求 : getQuick
    @RequestMapping(value = "/v3/collect-device-quickquerys", method = RequestMethod.POST)
    //endregion
    public CollectDeviceQuickqueryModel getQuick(Map<String, String> header, Map<String, Object> query, MeterPlan_workOrderlistModel body) throws ServiceException {
        CollectDeviceQuickqueryModel result = new CollectDeviceQuickqueryModel();

        List<MeterPlan_workOrderItemModel> meterPlan_workOrderItemModels = body.getMeterPlanworkOrderList();
        if (body.getMeterPlanworkOrderList() != null && body.getMeterPlanworkOrderList().size() > 0) {
            List<CollectDeviceQuickqueryItemModel> list_1 = new ArrayList<>();

            String collectTime = HzhrTypeUtil.object2string(query.get("_equal_collectTime"));
            String startTime = collectTime;
            String endTime = null;
            if (!StringUtils.isEmpty(startTime)) {
                startTime = HzhrDateUtil.dateToStr(HzhrDateUtil.strToDate(startTime, "yyyy-MM-dd"));
                endTime = HzhrDateUtil.dateToStr(HzhrDateUtil.addDay(HzhrDateUtil.strToDate(startTime, "yyyy-MM-dd"), 1));
            }
            List<CollectDeviceEntityExt> collectDeviceEntityExts_1 = collectDeviceEntityCustomMapper.selectQuick(body, startTime, endTime);

            //List<CollectDeviceEntityExt> collectDeviceEntityExts_2 = collectDeviceEntityCustomMapper.selectQuick1(body);
            CollectDeviceQuickqueryModel collectDeviceQuickqueryModel_2 = new CollectDeviceQuickqueryModel();
            for (CollectDeviceEntityExt collectDeviceEntityExt_3 : collectDeviceEntityExts_1){
                String gasuserCode1 = collectDeviceEntityExt_3.getGasuserCode();
                for (MeterPlan_workOrderItemModel meterPlan_workOrderItemModel : meterPlan_workOrderItemModels){
                    String gasuserCode2 = meterPlan_workOrderItemModel.getGasUserCode();
                    if (gasuserCode1 != null && gasuserCode1.equals(gasuserCode2)){
                        //返回结果
                        CollectDeviceQuickqueryItemModel collectDeviceQuickqueryModel = new CollectDeviceQuickqueryItemModel();
                        collectDeviceQuickqueryModel.setId(collectDeviceEntityExt_3.getId());
                        collectDeviceQuickqueryModel.setCurrentTm(collectDeviceEntityExt_3.getCurrentTm());
                        collectDeviceQuickqueryModel.setCurrentValue(collectDeviceEntityExt_3.getCurrentValue());
                        collectDeviceQuickqueryModel.setCreateTime(collectDeviceEntityExt_3.getCreateTime());
                        collectDeviceQuickqueryModel.setCode(collectDeviceEntityExt_3.getCode());
                        collectDeviceQuickqueryModel.setGasuserCode(collectDeviceEntityExt_3.getGasuserCode());
                        list_1.add(collectDeviceQuickqueryModel);
                        break;
                    }
                }
            }

            result.setList(list_1);

            //返回结果
//        List<CollectDeviceQuickqueryItemModel> list_1 = new ArrayList<>();
//        CollectDeviceQuickqueryModel result = new CollectDeviceQuickqueryModel();
//        CollectDeviceQuickqueryItemModel collectDeviceQuickqueryModel = new CollectDeviceQuickqueryItemModel();
//        for (CollectDeviceEntityExt collectDeviceEntityExt_3 : collectDeviceEntityExts_1){
//            collectDeviceQuickqueryModel.setId(collectDeviceEntityExt_3.getId());
//            collectDeviceQuickqueryModel.setCurrentTm(collectDeviceEntityExt_3.getCurrentTm());
//            collectDeviceQuickqueryModel.setCurrentValue(collectDeviceEntityExt_3.getCurrentValue());
//            collectDeviceQuickqueryModel.setCreateTime(collectDeviceEntityExt_3.getCreateTime());
//            list_1.add(collectDeviceQuickqueryModel);
//
//        }
        } else {
            result.setList(new ArrayList<>());
        }

        return result;
    }

}

